package game;
/*
 * Another thread that generates different kinds of enemies
 */

import java.util.*;
import java.lang.Math;

import game.CountDown;
import Missiles.Missile;

import enemies.*;

public class EnemyGenerator implements Runnable, Constants  
{
	boolean stopThread;
	public ArrayList<BasicEnemy> easyEnemies;
	public ArrayList<Missile> enemyMissiles;
	public ArrayList<Explosion> explosions;
	public Player target;
	public int enemyGenerated = 0;  //if enemyGenerated reaches NUMBER_OF_ENEMIES, a BOSS will appear
	public double[] rate; // random<rate1: easyEnemies; rate1<random<rate2: strong Enemies, and so on
	
	private double r;
	private int sleepTime;
	private final int TYPE_OF_ENEMY = 4; //number of types of enemies except boss
	
	public EnemyGenerator(Player player, ArrayList<Explosion> expl)
	{
		stopThread = false;
		easyEnemies = new ArrayList<BasicEnemy>();
		enemyMissiles = new ArrayList<Missile>();
		target = player;
		explosions = expl;
		rate = new double[TYPE_OF_ENEMY];
		sleepTime = 900;
	}
	
	private int generateX()
	{	return (int)(Math.random() * (MAIN_WIDTH - 100)) + 50;		}
	
	public void generateEasyEnemy()
	{
		easyEnemies.add(new BasicEnemy(generateX()));
		enemyGenerated ++;
	}
	
	public void generateStrongEnemy()
	{
		easyEnemies.add(new StrongEnemy(generateX(), enemyMissiles));
		enemyGenerated += 2;
	}
	
	public void generateSuperEnemy()
	{
		easyEnemies.add(new SuperEnemy(generateX(), enemyMissiles));
		enemyGenerated += 3;
	}
	
	public void generateSlower()
	{
		if (Math.random() < 0.5)
			easyEnemies.add(new Slower(1, enemyMissiles));
		else easyEnemies.add(new Slower(MAIN_WIDTH, enemyMissiles));
	}
	
	public void run()
	{
		try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            System.out.println("interrupted");
        }
		rate[1] = 0.75; rate[2] = 0.9; rate[3] = 0.95; //can be modified for the purpose of testing
		
		while (!stopThread)
		{
			if (enemyGenerated > 80){ 
				rate[1] = 0.1; rate[2] = 0.2; rate[3] = 0.3; sleepTime = 550;}
			else if (enemyGenerated > 40){
				rate[1] = 0.2; rate[2] = 0.4; rate[3] = 0.6; sleepTime = 700;}
			else if (enemyGenerated > 10){
				rate[1] = 0.5; rate[2] = 0.75; rate[3] = 0.9; sleepTime = 800;}
			
			r = Math.random();
			
			if (r < rate[1])
				generateEasyEnemy();
			else if (r < rate[2])
				generateStrongEnemy();
			else if (r < rate[3])
				generateSlower();
			else
				generateSuperEnemy();
			
			try {
	            Thread.sleep(1500);
	        } catch (InterruptedException e) {
	            System.out.println("interrupted");
	        }
			
			if (enemyGenerated >= NUMBER_OF_ENEMIES)
			{
				Thread count = new Thread(new CountDown(7));
				count.start();
				try {	count.join();	} //synchronize
				catch (InterruptedException e) {	System.out.println("interrupted");	}
				easyEnemies.add(new Boss(enemyMissiles, easyEnemies, target, explosions));
				break;  //The player wins if boss is defeated, so there is no need to spawn enemies any more.
			}
		}
	}
}
